<template>
  <div class="app-container">
    <el-dialog id="dialogContainer"
      title="巡查码"
      :visible.sync="dialogVisible"
      width="15%" >
      <div  id="qrcode"  ref="qrcode" ></div>
<!--       flex-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
<!--      <el-form-item label="用户id" prop="postId">-->
<!--        <el-input-->
<!--          v-model="queryParams.postId"-->
<!--          placeholder="请输入用户id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="巡查类型" prop="codeType">
<!--        <el-select v-model="queryParams.codeType" placeholder="请选择巡查类型" clearable size="small">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
        <el-select v-model="queryParams.codeType" placeholder="请选择巡查类型" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:code:add']"
        >新增巡查码</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:code:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:code:remove']"
        >删除巡查码</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:code:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="codeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="codeId" />
<!--      <el-table-column label="用户id" align="center" prop="postId" />-->
      <el-table-column label="巡查类型" align="center" prop="dictValue" :formatter="typeFormat" />
      <el-table-column label="字典键值" align="center" prop="codeType" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:code:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:code:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-eye"
            @click="lookCode(scope.row)"
            v-hasPermi="['system:code:look']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡查码对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="用户id" prop="postId">-->
<!--        <el-input v-model="form.postId" placeholder="请输入用户id" />-->
<!--      </el-form-item>-->
        <el-form-item label="巡查类型">
<!--          <el-select v-model="form.codeType" placeholder="请选择巡查类型">-->
<!--            <el-option label="请选择字典生成" value="" />-->
<!--          </el-select>-->
          <el-select v-model="form.codeType" placeholder="请选择巡查类型" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
  import { listCode, getCode, delCode, addCode, updateCode, exportCode } from "@/api/system/code";
  import QRCode  from "qrcodejs2"

  import {listType,getType} from "@/api/system/dict/type";

  export default {
    name: "Code",
    data() {
      return {
        //默认关闭二维码弹出框
        dialogVisible : false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 巡查码表格数据
        codeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          postId: undefined,
          codeType: undefined,
          address: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    components: { QRCode },
    created() {
      this.getList();
      // this.getType()
    //  code_type
      this.getDicts("code_type").then(response => {
        this.statusOptions = response.data;
        console.log(response)
      });
    },
    methods: {
      // 岗位状态字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.codeType);
      },
      //查询类型字典
      getType(){
        let sys_patrol_type = 14
        getType(sys_patrol_type).then(res=>{
          console.log(res);
        }).catch(err=>{
          console.log(err);
        })
      },
      //查看二维码
      lookCode(row){
        this.dialogVisible=true
        this.$nextTick (function () {
           document.getElementById('qrcode').innerHTML = ''
          this.qrcode(row.codeId)
        })
      },
      qrcode(e){
        e = e.toString()
        console.log( e)

        let qrcode = new QRCode('qrcode', {
          width: 150,
          height: 150, // 高度
          text: e, // 二维码内容
          // render: 'canvas' ,// 设置渲染方式（有两种方式 table和canvas，默认是canvas）
          // background: '#f0f',
          // foreground: '#ff0',
        })
      },
      /** 查询巡查码列表 */
      getList() {
        this.loading = true;
        console.log(this.queryParams);
        listCode(this.queryParams).then(response => {
          console.log(response.rows)
          this.codeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          codeId: undefined,
          postId: undefined,
          codeType: undefined,
          createTime: undefined,
          address: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.codeId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加巡查码";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const codeId = row.codeId || this.ids
        getCode(codeId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改巡查码";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.codeId != undefined) {
              updateCode(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addCode(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const codeIds = row.codeId || this.ids;
        this.$confirm('是否确认删除巡查码编号为"' + codeIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCode(codeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有巡查码数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCode(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    }
  };
</script>
<style scoped>
  .flex{display: flex;justify-content: space-between;align-items: center;flex-wrap: nowrap;}
  #qrcode{width: 150px;height: 150px;}
  /*#dialogContainer .el-dialog__body{display: flex!important;justify-content: space-around!important;align-items: cente!important;flex-wrap: nowrap!important;}*/
  /*#dialogContainer{width: 220px}*/
</style>
